<?php

class Technical_FacultyController extends Zend_Controller_Action
{

	public function init()
	{
		/* Initialize action controller here */
	}

	public function indexAction()
	{
		$session = new Zend_Session_Namespace('identity');
		$faculties = new Application_Model_Dao_Faculty();
		$result = $faculties->getAll()->toArray();
		$this->view->faculties = $result;
		if($result == null){
			$this->view->empty = "Brak wydziałów w bazie.";
		}else{
			$this->view->empty = "";
		}
		$designation = $this->_getParam('designation');
		if(isset($session->statusdel) && $this->_getParam('statusdelete') == 'true') {
			$this->view->delete = "Rekord \"".$designation."\" został poprawnie usunięty.";
			unset($session->statusdel);
		}else{
			$this->view->delete = "";
		}

		if( isset( $session->attention ) ) {
			$this->view->attention = $session->attention ;
			unset( $session->attention );
		} else {
			$this->view->attention = "";
		}

		if( isset( $session->operationsuccess ) ) {
			$this->view->operationStatus = $session->operationsuccess ;
			unset( $session->operationsuccess );
			if(isset( $session->operationdesignation ) ) {
				$this->view->operationDesignation = $session->operationdesignation;
				unset( $session->operationdesignation );
			} else {
				$this->view->operationDesignation = "";
			}
		} else {
			$this->view->deleteRecord = "";
		}
		$form = new Application_Form_Faculty();
		$form->appendValidationEngineFiles($this->view);
		$polish = BH_TranslatePL::getPolishTranslation();
		$translate = new Zend_Translate('array', $polish, 'pl');
		$form->setTranslator($translate);
		// edycja oraz dodawanie w jquery, kod obsługujący requesta z danymi z formularza, zapis do bazy
		if( $this->getRequest()->isPost() ) {
			$formData = $this->getRequest()->getPost();
			if( $form->isValid($formData) ) {
				$faculty = new Application_Model_Entities_Faculty();
				$faculty->setId( $form->getValue('id'));
				$faculty->setDesignation( $form->getValue('designation'));
				$faculty->setShort( $form->getValue('short'));
				$facultyDao = new Application_Model_Dao_Faculty();
				if($form->getValue('typeForm') == 'editform' ) {
					$result = $facultyDao->updateFaculty($faculty);
					if($result > 0 ) {
						$session->operationsuccess = 'updatesuccess';
					} else {
						$session->operationsuccess = 'updatefail';
					}
				} else if ($form->getValue('typeForm') == 'addform' ) {

				} else {
					$session->attention = 'Błędne dane dotyczące typu operacji';
				}
				$this->_helper->redirector('index');
			} else {
				$form->populate($formData);
			}
		} else {
			$session->attention = '';
		}
	}


	public function addAction(){
		$session = new Zend_Session_Namespace('identity');
		$form = new Application_Form_Faculty();
		$form->appendValidationEngineFiles($this->view);
		$polish = BH_TranslatePL::getPolishTranslation();
		$translate = new Zend_Translate('array', $polish, 'pl');
		$form->setTranslator($translate);
		$form->getElement('typeForm')->setValue('addform');
		$form->submit->setLabel('Dodaj nowy wydział');
		$this->view->FacultyForm = $form;
		$request = $this->getRequest();
		if ($request->isPost()) {
			if ($form->isValid($request->getPost())) {
				$faculty = new Application_Model_Entities_Faculty();
				$faculty->setDesignation( $form->getValue('designation'));
				$faculty->setShort( $form->getValue('short'));
				$facultyDao = new Application_Model_Dao_Faculty();
				$result = $facultyDao->saveFaculty($faculty);
				if($result > 0 ) {
					$session->operationsuccess = 'addsuccess';
					$session->operationdesignation = $form->getValue('designation');
				} else {
					$session->operationsuccess = 'addfail';
				}
				$this->_helper->redirector('index');
			} else {
				$session->attention = 'Przesłane dane w formularzu nie są poprawne.';
				$this->_helper->redirector( 'index');
			}
		} else {
			$session->attention = '';
			//$this->_helper->redirector( 'index');
		}
	}
	public function editAction(){
		$session = new Zend_Session_Namespace('identity');
		$form = new Application_Form_Faculty();
		$form->appendValidationEngineFiles($this->view);
		$polish = BH_TranslatePL::getPolishTranslation();
		$translate = new Zend_Translate('array', $polish, 'pl');
		$form->setTranslator($translate);
		$form->submit->setLabel('Zatwierdź zmiany');
		$this->view->FacultyEditForm = $form;
		if ($this->getRequest()->isPost()) {
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData)) {
				$faculty = new Application_Model_Entities_Faculty();
				$faculty->setId( $form->getValue('id'));
				$faculty->setDesignation( $form->getValue('designation'));
				$faculty->setShort( $form->getValue('short'));
				$facultyDao = new Application_Model_Dao_Faculty();
				$facultyDao->updateFaculty($faculty);
				$this->_helper->redirector('index');
			} else {
				$form->populate($formData);
			}
		}else {
			$id = $this->_getParam('id', 0);
			if ($id > 0) {
				$facultyDao = new Application_Model_Dao_Faculty();
				$form->populate($facultyDao->getFacultyByID($id));
				$session->operationdesignation = $form->getValue('designation');
				$form->getElement('typeForm')->setValue('editform');
			} else {
				$session->attention = 'Błąd podczas pobierania danych lub podczas przypisywania ich do pól formularza.';
				$this->_helper->redirector( 'index');
			}
		}
	}
	public function deleteAction(){
		$session = new Zend_Session_Namespace('identity');
		$request = $this->getRequest();
		if ($request->isPost()) {
			$del = $request->getPost('del');
			if ($del == 'Tak, usuń rekord') {
				$id = $request->getPost('id');
				$designation = $request->getPost('designation');
				$faculty = new Application_Model_Entities_Faculty();
				$faculty->setId($id);
				$facultyDao = new Application_Model_Dao_Faculty();
				$facultyDao->deleteFaculty($faculty);
				$session->statusdel = 'true';
				$this->_helper->redirector( 'index', 'faculty', 'technical', array('statusdelete' => 'true', 'designation' => $designation));
			}else {
				$this->_helper->redirector( 'index');
			}
		} else if($this->_getParam('jsflag') == 'false' ){
			$id = $this->_getParam('id', 0);
			if ($id > 0) {
				$facultyDao = new Application_Model_Dao_Faculty();
				$result = $facultyDao->getRowFaculty($id);
				$this->view->faculties = $result;
			} else {
				$session->attention = 'Błąd podczas pobierania danych lub podczas przypisywania ich do pól formularza.';
				$this->_helper->redirector( 'index');
			}

		} else if($this->_getParam('jsflag') == 'true' ){
			$id = $this->_getParam('id', 0);
			$faculty = new Application_Model_Entities_Faculty();
			$faculty->setId($id);
			$facultyDao = new Application_Model_Dao_Faculty();
			$result = $facultyDao->deleteFaculty($faculty);
			if($result > 0) {
				$session->operationsuccess = "deletesuccess" ;
				$session->operationdesignation = $this->_getParam('name', 0);
			} else {
				$session->operationdesignation = "";
				$session->operationsuccess = "deletefail" ;
			}

		} else {
			$this->_helper->redirector('index');
		}
	}

}
